﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SMIS.Entities;
using DX.CRC.Common.Entities;

namespace SMIS.Dals
{
    class Dal4Depart : DalBase
    {
        public static List<Depart> SearchAllDeparts()
        {
            QueryObject qo = CreateQO();

            qo.CmdText = @"SELECT [DID] as ID
                      ,[DName] as Name
                      ,[DLeader] as Leader
                      ,[CreateTime]
                      ,[IsActive]
                      ,[Remark] 
                  FROM [DepartInfo] where IsActive=1";

            return qo.xExecuteList<Depart>();
        }

        /// <summary>
        /// 【闻祖东 2013-4-8-193408】这个方法请用面向对象的方式传递参数。
        /// </summary>
        /// <param name="dName"></param>
        /// <param name="dLeader"></param>
        /// <param name="remark"></param>
        /// <returns></returns>
        public static List<Depart> InsertDepart(string dName, string dLeader, string remark)
        {
            QueryObject qo = CreateQO();

            qo.CmdText=@"INSERT INTO [DepartInfo]
                       ([DName]
                       ,[DLeader]
                       ,[CreateTime]
                       ,[IsActive]
                       ,[Remark])
                 VALUES
                       (@DName
                       ,@DLeader
                       ,GETUTCDATE()
                       ,1
                       ,@Remark)";

            qo.AddParam("@DName",dName);
            qo.AddParam("@DLeader",dLeader);
            qo.AddParam("@Remark",remark);

            return qo.xExecuteList<Depart>();
        }

        public static bool UpdateSelectedDepart(Depart depart)
        {
            QueryObject qo = CreateQO();

            qo.CmdText = @"declare @Count int
                           select @Count=COUNT(*) from [DepartInfo] 
                           where @DName in (select DName from DepartInfo where DID<>@DID)
                           if @Count=0
                           begin
                           UPDATE [DepartInfo]
                               SET [DName] = @DName
                                  ,[DLeader] = @DLeader
                                  ,[Remark] = @Remark
                           WHERE DID=@DID
                           end";

            qo.AddParam("@DName", depart.Name);
            qo.AddParam("@DLeader", depart.Leader);
            qo.AddParam("@Remark", depart.Remark);
            qo.AddParam("@DID", depart.ID);

            return qo.xExecuteNonQuery() == 1;
        }

        public static bool DeleteSelectedDepart(Depart depart)
        {
            QueryObject qo = CreateQO();

            qo.CmdText = @"declare @Count int
                           select @Count=COUNT(*) from DepartInfo D 
                           where @DName in (select S.DepName from StaffInfo S)
                           if @Count=0
                           begin
                           UPDATE [DepartInfo]
                           SET [IsActive] = 0
                           WHERE DID=@DID
                           end";

            qo.AddParam("@DName", depart.Name);
            qo.AddParam("@DID", depart.ID);

            return qo.xExecuteNonQuery() == 1;
        }
    }
}
